package com.trkj.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IBigOfficeDao;
import com.trkj.dao.mybatis.SickroomTableMapper;
import com.trkj.pojos.BigOffice;
import com.trkj.pojos.SmallOffice;
import com.trkj.vo.BigOfficeTypeVo;
import com.trkj.vo.SmallOfficeTypeVo;

@Service
@Transactional
public class BigOfficeService {
	@Autowired
	private  IBigOfficeDao bigOfficeDao;
	@Autowired
	private SickroomTableMapper stm;
	
	/**
	 * 查询住院科室
	 * @return
	 */
	public List<SmallOfficeTypeVo> queryLiveOffice(){
		String name = "住院科";
		
		BigOffice bigOffice = bigOfficeDao.findByName(name);
		List<SmallOffice> smallOffices = bigOffice.getSmallOffices();
		
		List<SmallOfficeTypeVo> smallOfficeTypeVos = new ArrayList<>();
		if(smallOffices!=null){
			for(SmallOffice smallOffice:smallOffices){
				SmallOfficeTypeVo smallOfficeTypeVo = new SmallOfficeTypeVo();
				smallOfficeTypeVo.setId(smallOffice.getName());
				smallOfficeTypeVo.setText(smallOffice.getName());
				smallOfficeTypeVos.add(smallOfficeTypeVo);
			}
		}
		return smallOfficeTypeVos;
		
	}
	/**
	 * 查询数所有的科室大类  封装成树形结构
	 * @return
	 */
	public List<BigOfficeTypeVo> findOfficeTree(){
		
		List<BigOffice> iterable = (List<BigOffice>) bigOfficeDao.findAll();
		
		if(iterable!=null){
			
			List<BigOfficeTypeVo> bigOfficeTypeVos = new ArrayList<>();
			
			for(BigOffice bigoffice :iterable ){
				
				BigOfficeTypeVo bigofficeVo = new BigOfficeTypeVo();
				bigofficeVo.setId(bigoffice.getBigOfficeId());
				bigofficeVo.setText(bigoffice.getName());
				
				List<SmallOfficeTypeVo> smallOfficeTypeVos = new ArrayList<>();
				List<SmallOffice> smallOffices = bigoffice.getSmallOffices();
				if(smallOffices!=null){
					for(SmallOffice smallOffice : smallOffices){
						SmallOfficeTypeVo smallOfficeTypeVo = new SmallOfficeTypeVo();
						smallOfficeTypeVo.setId(smallOffice.getName());
						smallOfficeTypeVo.setText(smallOffice.getName());
						smallOfficeTypeVos.add(smallOfficeTypeVo);
					}
				}
				bigofficeVo.setChildren(smallOfficeTypeVos);
				bigOfficeTypeVos.add(bigofficeVo);
			}
			return bigOfficeTypeVos;
		}
		return null;
	}

}
